package com.smg.securitydemo.controller;

import lombok.val;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {

    @GetMapping("/")
    public Map index() {
        SecurityContext context = SecurityContextHolder.getContext();//存储认证对象的上下文
        Authentication authenticationt=context.getAuthentication();//获取认证对象
        String username = authenticationt.getName();
        Object principal = authenticationt.getPrincipal();
        authenticationt.getCredentials();
        Collection<? extends GrantedAuthority > collections = context.getAuthentication().getAuthorities();
        HashMap result=new HashMap();

        //创建结果对象

        result.put("code", 0);
        result.put("data", username);

        return result;
    }
}